package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper mapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /*
     * 购物车添加数据
     * */
    public void add(ShoppingCartDTO dto) {
        //判断当前加入购物车的商品是否存在：存在-》数量+1 不存在-》插入数据
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        if (dto.getSetmealId() != null)
            queryWrapper.eq("setmeal_id", dto.getSetmealId());
        if (dto.getDishFlavor() != null)
            queryWrapper.eq("DISH_FLAVOR", dto.getDishFlavor());
        if (dto.getDishId() != null)
            queryWrapper.eq("DISH_ID", dto.getDishId());
        List<ShoppingCart> list = mapper.selectList(queryWrapper);

        if (list != null && !list.isEmpty()) {
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            mapper.updateById(shoppingCart);
        }else {//不存在-》插入数据
            ShoppingCart shoppingCart=new ShoppingCart();
            BeanUtils.copyProperties(dto,shoppingCart);
            Long dishId = dto.getDishId();
            Long setmealId = dto.getSetmealId();
            //判断本次添加的是菜品还是套餐
            if(dishId!=null){
                Dish dish=dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                Setmeal setmeal=setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            mapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> selectList() {
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",BaseContext.getCurrentId());
        return mapper.selectList(queryWrapper);
    }

    /*
    * 清空购物车
    * */
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        mapper.delete(queryWrapper);
    }

    /*
    * 减少购物车内商品数量/删除商品
    * */
    public void sub(ShoppingCartDTO dto) {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        if(dto.getSetmealId()!=null)
            queryWrapper.eq("setmeal_id",dto.getSetmealId());
        if(dto.getDishId()!=null)
            queryWrapper.eq("dish_id",dto.getDishId());
        if(dto.getDishFlavor()!=null)
            queryWrapper.eq("dish_flavor",dto.getDishFlavor());
        List<ShoppingCart> shoppingCarts = mapper.selectList(queryWrapper);
        ShoppingCart shoppingCart = shoppingCarts.get(0);

        if(shoppingCart.getNumber()==1)
            mapper.deleteById(shoppingCart.getId());
        else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            mapper.updateById(shoppingCart);
        }

    }
}
